package com.leetcode.LC;

public class LC1576 {
    public String modifyString(String s) {//错误理解题意

        String abc = "abcdefghijklmnopqrstuvwxyz";
        char str = 'a';
        for (int i = 0; i < abc.length(); i++) {
            if (!s.contains(abc.charAt(i) + "")) {
                str = abc.charAt(i);
                break;
            }
        }
        return s.replace('?', str);
    }

    public String modifyString2(String s) {
        if (s.length()==1&&s.equals("?")){
            return "a";
        }
        char[] chars = s.toCharArray();
        char str = 'a';

        if (chars[0] == '?') {//单独处理两边
            while (true) {
                if (str != chars[1]) {
                    chars[0] = str;
                    break;
                } else {
                    str++;
                    if (str > 'z') {
                        str = 'a';
                    }
                }
            }
        }
        if (chars[chars.length - 1] == '?') {
            while (true) {
                if (str != chars[chars.length - 2]) {
                    chars[chars.length - 1] = str;
                    break;
                } else {
                    str++;
                    if (str > 'z') {
                        str = 'a';
                    }
                }
            }
        }

        for (int i = 1; i < chars.length - 1; i++) {
            if (chars[i] == '?') {
                while (true) {
                    if (str != chars[i - 1] && str != chars[i + 1]) {
                        chars[i] = str;
                        break;
                    } else {
                        str++;
                        if (str > 'z') {
                            str = 'a';
                        }
                    }
                }
            }
        }
        return new String(chars);
    }

    public static void main(String[] args) {
        System.out.println(new LC1576().modifyString2("?a"));
    }
}
